package com.cmc.cloud.cmclink.doc.controller.admin.si;

import cn.hutool.core.bean.BeanUtil;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.service.si.BlnoScnService;
import com.cmc.cloud.cmclink.doc.vo.blnoscnvo.BlnoScnCreateReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnoscnvo.BlnoScnPageReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnoscnvo.BlnoScnRespVO;
import com.cmc.cloud.cmclink.doc.vo.blnoscnvo.BlnoScnUpdateReqVO;
import com.cmc.cloud.cmclink.doc.vo.blnoscnvo.LinkCpfCodeReqVO;
import com.cmes.framework.operatelog.core.annotations.OperateLog;
import com.cmes.framework.operatelog.core.enums.OperateTypeEnum;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import java.util.*;


import com.cmc.cloud.cmclink.doc.entity.BlnoScnDO;
import com.cmc.cloud.cmclink.doc.convert.BlnoScnConvert;


/**
 * 提单收发通信息前端接口Controller
 *
 * @author codegen
 */
@Tag(name = "管理后台 - 提单收发通信息")
@RestController
@Validated
@RequestMapping(UrlConstants.APPLICATION_URL + "/blnoScn")
public class BlnoScnController {

    @Resource
    private BlnoScnService blnoScnService;

    @Operation(summary = "创建提单收发通信息")
    @OperateLog(type = OperateTypeEnum.CREATE)
    @PostMapping("/create")
    @PreAuthorize("@ss.hasPermission('doc:blno-scn:create')")
    public CommonResult<Void> createBlnoScn(@Valid @RequestBody BlnoScnCreateReqVO createReqVO) {
        blnoScnService.createBlnoScn(createReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "更新提单收发通信息")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PostMapping("/update")
    @PreAuthorize("@ss.hasPermission('doc:blno-scn:update')")
    public CommonResult<Void> updateBlnoScn(@Valid @RequestBody BlnoScnUpdateReqVO updateReqVO) {
        blnoScnService.updateBlnoScn(updateReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "删除提单收发通信息")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PostMapping("/delete/{id}")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('doc:blno-scn:delete')")
    public CommonResult<Void> deleteBlnoScn(@PathVariable("id") Long id) {
        blnoScnService.deleteBlnoScn(id);
        return CommonResult.success();
    }


    @Operation(summary = "根据提单号获取提单收发通信息")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/getScnByBlNo")
    @Parameter(name = "blNo", description = "提单号", required = true)
    public CommonResult<List<BlnoScnRespVO>> getScnBy(@RequestParam("blNo") String blNo) {
        return CommonResult.success(BeanUtil.copyToList(blnoScnService.getScnByBlNo(blNo), BlnoScnRespVO.class));
    }

    @Operation(summary = "获得提单收发通信息")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('doc:blno-scn:query')")
    public CommonResult<BlnoScnRespVO> getBlnoScn(@PathVariable("id") Long id) {
        BlnoScnDO blnoScn = blnoScnService.getBlnoScn(id);
        return CommonResult.success(BlnoScnConvert.INSTANCE.convert(blnoScn));
    }

    @Operation(summary = "获得提单收发通信息列表")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/list")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('doc:blno-scn:query')")
    public CommonResult<List<BlnoScnRespVO>> getBlnoScnList(@RequestParam("ids") Collection<Long> ids) {
        List<BlnoScnDO> list = blnoScnService.getBlnoScnList(ids);
        return CommonResult.success(BlnoScnConvert.INSTANCE.convertList(list));
    }

    @Operation(summary = "获得提单收发通信息分页")
    @OperateLog(type = OperateTypeEnum.GET)
    @GetMapping("/page")
    @PreAuthorize("@ss.hasPermission('doc:blno-scn:query')")
    public CommonResult<PageResult<BlnoScnRespVO>> getBlnoScnPage(@Valid BlnoScnPageReqVO pageVO) {
        PageResult<BlnoScnDO> pageResult = blnoScnService.getBlnoScnPage(pageVO);
        List<BlnoScnRespVO> results = BlnoScnConvert.INSTANCE.convertList(pageResult.getList());
        return CommonResult.success(new PageResult<>(results, pageResult.getTotal()));
    }

    @Operation(summary = "绑定收发通cpfCode")
    @PostMapping("/bindCpfCode")
    public CommonResult<String>  bindCpfCode(@RequestBody @Valid LinkCpfCodeReqVO linkCpfCodeReqVO){
        return CommonResult.success( blnoScnService.bindCpfCode(linkCpfCodeReqVO));
    }
}
